import React from 'react';
import {Link} from 'react-router-dom';
import { Button, Row, Col, Divider, Popconfirm, message, Dropdown, Menu, Icon} from 'antd';
import axios from 'axios';
import {withRouter} from 'react-router-dom';
import Grid from '../../components/table/grid';
import RoleManager from './roleManager';
import {injectIntl} from 'react-intl';


@withRouter
class ChildAccountList extends React.Component {
    constructor(props) {
        super(props);
        const common = this.props.intl.messages.common;
        this.columns = [
            {
                title: common.sequenceNO,
                dataIndex: 'rowno',
            },
            {
                title: common.loginName,
                dataIndex: 'account',
                width: '20%'
            },
            {
                title: common.name,
                dataIndex: 'name',
                render: (text, record) => (
                    <span>
                        {(record.name == null || record.name == '') ? record.account : record.name}
                    </span>
                )
            },
            {
                title: common.remark,
                dataIndex: 'createTime',
                width: '20%'
            },
            {
                title: common.status,
                dataIndex: 'status',
            },
            {
                title: common.operation,
                width: '20%',
                key: 'action',
                render: (text, record) => (
                    <span>
                        <Popconfirm title={`${common.DoResetIt}？`} okText={common.sure}
                                    onConfirm={() => this.resetPassword(record.id)}
                                    cancelText={common.cancel}>
                            <a>{common.resetPassword}</a>
                        </Popconfirm>
                        <Divider type="vertical"/>
                        <Popconfirm placement="top" title={`${common.VerifyPerformed}?`} okText={common.sure}
                                    onConfirm={() => this.modStatus(text, record)}
                                    cancelText={common.cancel}>
                            <a>
                                {((record) => {
                                    if (record.status === common.normal) {
                                        return common.forbidden
                                    }
                                    if (record.status === common.forbidden) {
                                        return common.enabled
                                    }
                                })(record)}
                            </a>
                        </Popconfirm>
                        <Divider type="vertical"/>
                        <Dropdown overlay={(
                            <Menu>
                                <Menu.Item>
                                    <a onClick={() => this.showModalX('roleManager', true, record.id, record)}>{common.authorityConf}</a>
                                </Menu.Item>
                            </Menu>
                        )}>
                        <a>{common.more} <Icon type="down"/></a>
                        </Dropdown>
                    </span>
                )
            }
        ];
        this.state = {
            params: {},
            roleManager: false,
            userId: '',
            refresh: 0,
            subAdminId: ''
        };
    }

    modStatus = (text) => {
        const common = this.props.intl.messages.common;
        let params = {
            id: text.id,
            status: text.status === common.normal ? 2 : 1
        }
        axios.post('/api/corp/subAcc/stat/mod', {...params}).then(res => {
            if (res) {
                this.setState({
                    refresh: this.state.refresh + 1
                });
            }
        });

    };
    //接口数据转换
    transformData = (data) => {
        const common = this.props.intl.messages.common;
        data.forEach((item) => {
            if (item.status == 1) {
                item.status = common.normal
            }
            if (item.status == 2) {
                item.status = common.forbidden
            }
        })
        return data
    };

    delete(id, account) {
        axios.post('/api/corp/subAcc/del', {
            id: id,
            account: account
        }).then(res => {
            this.setState({
                refresh: this.state.refresh + 1
            });
        });
    }

    resetPassword(id) {
        axios.post('/api/corp/psw/reset', {
            id: id
        }).then(res => {
            message.success('重置成功');
        });
    }

    showModalX(key, value, subAdminId, record) {
        this.setState({
            [key]: value,
            subAdminId: subAdminId,
            selectInfo: record
        })
    }


    showModal(key, value, isRefresh) {
        this.setState({
            [key]: value,
            refresh: this.state.refresh + 1
        })
    }

    render() {
        const common = this.props.intl.messages.common;
        return (
            <div>
                <Row type="flex" justify="end" style={{marginBottom: 20}}>
                    <Col>
                        <Link to="/company/addAccount">
                            <Button type="primary">{common['addSub-account']}</Button>
                        </Link>
                    </Col>
                </Row>
                <Grid transformData={this.transformData} refresh={this.state.refresh} params={this.state.params}
                      columns={this.columns}
                      url="/api/corp/subAcc/qry"/>
                <RoleManager visible={this.state.roleManager} subAdminId={this.state.subAdminId}
                             selectInfo={this.state.selectInfo}
                             close={() => this.showModal('roleManager', false, true)}/>
            </div>
        )
    }
}

export default injectIntl(ChildAccountList);